﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BCDataCenter;
public partial class ControlUser_SaleOnline : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadInFoBA();
            if (Session["Cart"] == null)
            {
                Session["Cart"]=new List<Inmast>();
            }
        }
    }
    int check(string proid)
    {
        int n = -1;
        if (Session["Cart"] != null)
        {
     
            for (int i = 0; i < ((List<Inmast>)Session["Cart"]).Count; i++)
            {
                if (proid == ((List<Inmast>)Session["Cart"])[i].Itemkey.ToString())
                {
                    n = i;
                }

            }
           
        }
        return n;
    }

    void InsertToSession(string Itemkey, string Desc, int Quantity, double Price)
    {
        Inmast Cart = new Inmast();
        Cart.Itemkey = Itemkey;
        Cart.Desc = Desc;
        Cart.Quantity = Quantity;
        Cart.Price = Price;
        int n = check(Cart.Itemkey.ToString());
        if (n < 0)
        {

            ((List<Inmast>)Session["Cart"]).Add(Cart);
        }
        else
        {
            (((List<Inmast>)Session["Cart"])[n]).Quantity += Cart.Quantity;
        }
       

    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "AddToCart")
        {
            string Itemkey = DataList1.DataKeys[e.Item.ItemIndex].ToString();
            Label LblDesc = (Label)e.Item.FindControl("lblDesc");
            Label LblPrice = (Label)e.Item.FindControl("lblPrice");
            if ((LblPrice.Text == "") || LblPrice.Text == null)
            {
                LblPrice.Text = "0";
            }
            InsertToSession(Itemkey, LblDesc.Text, 1, double.Parse(LblPrice.Text));
        }
    }
    protected void btSearch_Click(object sender, EventArgs e)
    {
        BindToShow();
    }
    protected void btXem_Click(object sender, EventArgs e)
    {
        if (Session["Cart"] != null)
        {
            Response.Redirect(Globals.ApplicationPath + "WebMaster/Administrator/SaleOnlineStep1/Default.aspx");
        }
        else
        {

        }
    }
    void BindToShow()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "ls_findItem", CommandType.StoredProcedure,
            new SqlParameter("@cont",txtFilter.Text));
        if (db.Rows.Count > 0)
        {
            DataList1.DataSource = db;
            DataList1.DataBind();
        }
    }
    void LoadInFoBA()
    {
        if (Session["Salesp_key_Logon"] != null)
        {
            dt = new ClassExecute.ClassExecute();
            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getInforBASaleOnline", CommandType.StoredProcedure,
                new SqlParameter("@salesp_key", Session["Salesp_key_Logon"].ToString()));
            txtMaTuVanVien.Text = Session["Salesp_key_Logon"].ToString();
            txtTenTuVanVien.Text = db.Rows[0]["salesp_name"].ToString();
        }
    }
}